﻿using AutoMapper;
using B.S.DataBase.Myproject.Helper;
using B.S.DataBase.MyProject.Domain.wdx;
using B.S.DataBase.MyProject.Domain.wdx.ViewModels;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure;
using Dm.util;
using MediatR;
using MedicalPCTerminal.Read.API.Appcation.Commands.ResidentCommand;
using SqlSugar;
using System.Net;

namespace MedicalPCTerminal.Read.API.Appcation.CommandHandlers.ResidentCommandHandler
{
    public class GetResidentFilesListCommandHandler : IRequestHandler<GetResidentFilesListCommand, ApiPaging<View_ResidentFileList>>
    {
        /// <summary>
        /// SqlSugar
        /// </summary>
        private readonly SqlSugarContext sqlSugar;
        private readonly IMapper mapper;

        public GetResidentFilesListCommandHandler(SqlSugarContext sqlSugar, IMapper mapper)
        {
            this.sqlSugar = sqlSugar;
            this.mapper = mapper;
        }

        public async Task<ApiPaging<View_ResidentFileList>> Handle(GetResidentFilesListCommand request, CancellationToken cancellationToken)
        {
            ApiPaging<View_ResidentFileList> result = new ApiPaging<View_ResidentFileList>();
            var list = sqlSugar.db.Queryable<ResidentFiles>()
               .LeftJoin<NCDSign>((file, sign) => file.ResidentFiles_Id == sign.Patient_Id)//多个条件用&&
               .Select((file, sign) => new View_ResidentFileList
               {
                   ResidentFiles_Id = file.ResidentFiles_Id,
                   ResidentFiles_Name = file.ResidentFiles_Name,
                   Sex = file.Sex == true ? "男" : "女",
                   IdCard = file.IdCard,
                   Age = file.Age,
                   Address = file.Address,
                   Phone = file.Phone,
                   Birthday = file.Birthday.ToString(),
                   CreateTime = file.CreateTime,
                   Sign_Status = sign.Sign_Status,
                   Doctor = sign.Doctor,
                   Team = sign.Team,
                   Hypertension= file.Hypertension
               });
            if (request.ResidentFiles_Name.IsNotNull())
            {
                // 将x改成file
                list = list.Where(file => file.ResidentFiles_Name.Contains(request.ResidentFiles_Name));
            }
            if (request.Hypertension!=null)
            {
                list = list.Where(file => file.Hypertension == request.Hypertension);
            }
            if (request.CreateStartTime.IsNotNull() && request.CreateEndTime.IsNotNull())
            {
                // 将x改成file
                list = list.Where(file => file.CreateTime >= request.CreateStartTime.StringToDateTime() && file.CreateTime <= request.CreateEndTime.StringToDateTime().AddDays(1).AddMilliseconds(-1.0));
            }
            int TotalCount = list.Count();
            int TotalPage = (int)Math.Ceiling(TotalCount * 1.0 / request.PageSize);
            list = list.OrderByDescending(file => file.CreateTime).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize);
            result.list = list.ToList();
            // 在查询结果之后进行日期格式化
            foreach (var item in result.list.ToList())
            {
                item.Birthday = item.Birthday.StringToDateTime().DateTimeToString("yyyy-MM-dd HH:mm:ss", "--");
            }
            result.TotalCount = TotalCount;
            result.TotalPage = TotalPage;
            result.Code = APIEnum.查询成功;
            result.Msg = APIEnum.查询成功.ToString();
            return await Task.FromResult(result);
        }
    }
}
